/*
 * Loader.cpp
 *
 *  Created on: May 4, 2011
 *      Author: alki
 */

#include "Loader.h"
#include "../Exception.h"

#include "LoaderAC.h"
#include "LoaderOBJ.h"

void Loader::strsplit(string const& str,const char delemiter,vector<string> &destination) {
	string::size_type last_position(0);
	string::size_type position(0);

	for (string::const_iterator it(str.begin()); it != str.end(); ++it, ++position) {
		if (*it == delemiter) {
			destination.push_back(str.substr(last_position, position - last_position));
			last_position = position + 1;
		}
	}
	destination.push_back(str.substr(last_position, position - last_position));
}

bool Loader::streq(const string& str1, const string& str2) {
    if (str1.size() != str2.size()) {
        return false;
    }
    for (string::const_iterator c1 = str1.begin(), c2 = str2.begin(); c1 != str1.end(); ++c1, ++c2) {
        if (tolower(*c1) != tolower(*c2)) {
            return false;
        }
    }
    return true;
}

bool Loader::strbegins(const string& str1, const string& str2) {
    for (string::const_iterator c1 = str1.begin(), c2 = str2.begin(); c2 != str2.end(); ++c1, ++c2) {
        if (tolower(*c1) != tolower(*c2)) {
            return false;
        }
    }
    return true;
}

Loader::Loader() {
}

Loader::~Loader() {
}

void Loader::LoadFile(string filename,vector<Objekt> &objekte,MaterialManager &matman) {
	vector<string> split;
	strsplit(filename,'.',split);

	Loader *fileloader=NULL;

	if(streq(split[split.size()-1],"ac")) {
		fileloader=new LoaderAC();
	}
	else if(streq(split[split.size()-1],"obj")) {
		fileloader=new LoaderOBJ();
	}

	if(fileloader==NULL)
		throw new Exception(NULL,__FILE__,__LINE__,"Fehler beim Laden des Modells","Dateiformat wird nicht unterstützt!");

	fileloader->LoadFile(filename,objekte,matman);
	delete fileloader;
}
